﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;

namespace OpenESSOConfigurator.PresentationLayer.UserControls
{
    public partial class DirectoryStoreView : UserControl
    {
        private DirectoryEntry Base;


        public DirectoryStoreView()
        {
            InitializeComponent();
        }

        private void toolStripButton1_2_Clicked(object sender, EventArgs e)
        {
            if (this.toolStripButton1.Text.Equals(">>"))
            {
                this.splitContainer2.SplitterDistance = this.splitContainer2.Width-32;
                this.toolStripButton1.Text = "<<";
                this.toolStripButton2.Text = "<<";
                this.toolStripContainer1.LeftToolStripPanelVisible = true;
            }
            else
            {
                this.splitContainer2.SplitterDistance = this.splitContainer2.Width-250;
                this.toolStripButton1.Text = ">>";
                this.toolStripButton2.Text = ">>";
                this.toolStripContainer1.LeftToolStripPanelVisible = false;
            }
        }

        private void toolStripButton3_Clicked(object sender, EventArgs e)
        {
            //Pass Connet info to DirextoryEntry object:
            Base = new DirectoryEntry("LDAP://10.208.239.49:389/dc=wacdev,dc=wacdomain,dc=axa", "cn=adminwac,cn=users,dc=wacdev,dc=wacdomain,dc=axa", "W@C5rv", AuthenticationTypes.ServerBind);
            //Read the root:
            if (Base != null)
            {
                this.treeView1.Nodes.Clear();
                this.treeView1.BeginUpdate();

                TreeNode childNode = this.treeView1.Nodes.Add(Base.Name,Base.Path,0);
                childNode.Tag = Base;
                try
                {
                    foreach (DirectoryEntry rootIter in Base.Children)
                    {
                        TreeNode RootNode = childNode.Nodes.Add(rootIter.Name);
                        RootNode.Tag = rootIter;
                        RootNode.ImageIndex = 1;
                        RootNode.SelectedImageIndex = 1;

                    }
                }
                catch(System.Exception ex)
                {
				    MessageBox.Show(ex.Message);
			    }
                finally
                {
                    childNode.Expand();
                    this.treeView1.EndUpdate();
                }
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //Fill the TreeView dynamic after Click
			if( e.Node.Nodes.Count == 0 )
			{
				DirectoryEntry parent = (DirectoryEntry)e.Node.Tag;
				if(parent != null)
				{
					if(parent.Children != null)
					{
						foreach(DirectoryEntry Iter in parent.Children)
						{
							TreeNode childNode = e.Node.Nodes.Add(Iter.Name);
							childNode.Tag = Iter;
                            childNode.ImageIndex = 1;
                            childNode.SelectedImageIndex = 1;
						}
						
					}
				}
			}

			//Fill the ListView Element
			try
			{
				DirectoryEntry list =(DirectoryEntry)e.Node.Tag;
				if(list!=null)
				{
					this.listView1.Clear();
					//Add some information to ListView ELement
					this.listView1.Columns.Add("attribute",90,HorizontalAlignment.Left);
					this.listView1.Columns.Add("value",350,HorizontalAlignment.Left);

					foreach(object listIter in list.Properties.PropertyNames)
					{
						foreach(Object Iter in list.Properties[listIter.ToString()])
						{
						System.Windows.Forms.ListViewItem item = new System.Windows.Forms.ListViewItem(listIter.ToString(),0);
						item.SubItems.Add(Iter.ToString());
							this.listView1.Items.AddRange( new ListViewItem[] {item});
						}
					}
				}
			}
			catch(System.Exception ex){
				
				MessageBox.Show(ex.Message);
			}	
		}
    }
}
